# we want to a a calculator that can do this following
# 1. add
# 2. subtract
# 3. multiply
# 4. divide

# it should get user input 
# it should print the result 

#we also want to continually ask for input
# a user can do multiple calculations per run 
# a user can quit by typing Q or q.

def calculator
  loop do
    puts "请选择操作:"
    puts "1. 加法"
    puts "2. 减法"
    puts "3. 乘法"
    puts "4. 除法"
    puts "输入 Q 或 q 退出"

    choice = gets.chomp
    

    if ['Q', 'q'].include?(choice)
      puts "感谢使用,再见!"
      break
    end

    unless ['1', '2', '3', '4'].include?(choice)
      puts "无效的选择,请重试。"
      next
    end

    print "请输入第一个数字: "
    num1 = gets.chomp.to_f
    print "请输入第二个数字: "
    num2 = gets.chomp.to_f

    result = case choice
    when '1'
      num1 + num2
    when '2'
      num1 - num2
    when '3'
      num1 * num2
    when '4'
      if num2.zero?
        "错误: 除数不能为零"
      else
        num1 / num2
      end
    end

    puts "结果: #{result}"
    puts "\n"
  end
end

calculator()